package com.thtfpc.stroke.screening.config;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoggerInterceptor implements HandlerInterceptor {


    Logger logger = LoggerFactory.getLogger(LoggerInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        logger.info("请求URL : " + httpServletRequest.getRequestURL());
        logger.info("请求Method : " + httpServletRequest.getMethod());
        logger.info("请求IP : " + httpServletRequest.getRemoteAddr());
//        logger.info("请求方法 : " + httpServletRequest.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

        // 获取参数, 只取自定义的参数, 自带的HttpServletRequest, HttpServletResponse不管
//        if (joinPoint.getArgs().length > 0) {
//            for (Object o : joinPoint.getArgs()) {
//                if (o instanceof HttpServletRequest || o instanceof HttpServletResponse) {
//                    continue;
//                }
//                logger.info("请求参数 : " + JSON.toJSONString(o));
//            }
//        }
    }
}
